package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;

    //列表显示
    @RequestMapping("/list")
    public String list(Model model, DestinationQueryObject qo){
        Page<Destination> page = destinationService.query(qo);
        model.addAttribute("page", page);
        model.addAttribute("qo", qo);
        model.addAttribute("toasts",destinationService.queryToasts(qo.getParentId()));
        return "/destination/list";
    }

    //添加数据
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Destination destination){
        destinationService.saveOrUpdate(destination);
        return JsonResult.success();
    }

    //关联目的地
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests() {
        List<Destination> destinations = destinationService.selectAll();
        System.out.println(destinations);
        return destinations;

    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        destinationService.delete(id);
        return JsonResult.success();
    }




}
